<template>
	<view class="page">
<!-- 		<view class="form-item">
			<text>用户名称</text>
			<input v-model="realName" placeholder="请输入真实姓名"  />
		</view> -->
					<view class="uni-form-item uni-column">
						<view class="title">请输入真实姓名</view>
						<input class="uni-input" focus placeholder="自动获得焦点"  v-model="realName"/>
					</view>
		<!-- <input class="uni-input" v-model="realName"  placeholder="请输入真实姓名" /> -->
		
<!-- 		<view class="form-item">
			<text>公司名称</text>
			<input v-model="company" placeholder="请输入公司名称" />
		</view> -->
		
		<!-- 公司 -->
<!-- 		<view class="uni-list">
		<picker @change="bindPickerChange3" :value="index3" :range="companys" >
			<view class="uni-input" :class="index3 == 0? 'uni-input':''">{{companys[index3]}}</view>
		</picker>
		</view> -->
		
		<view class="divider"/>
		
		<!-- 公司 -->
		
		<view class="uni-list">
			<view style="margin-top: 30px;"> 公司 </view>
		<picker @change="bindPickerChange3" :value="index3" :range="companys" >
			<view class="uni-input" :class="index3 == 0? 'uni-input':''" style="width: 70%">{{companys[index3]}}</view>
		</picker>
		</view>
		
<!-- 				<view class="uni-title uni-common-pl">公司</view>
				<view class="uni-list">
					<view class="uni-list-cell">
						<view class="uni-list-cell-left">
							当前选择
						</view>
						<view class="uni-list-cell-db">
							<picker @change="bindPickerChange3" :value="index3" :range="companys">
								<view class="uni-input">{{companys[index3]}}</view>
							</picker>
						</view>
					</view>
				</view> -->
<view class="divider"/>
		
		<!-- 部门 -->
		<view class="uni-list">
			<view style="margin-top: 30px;">部门</view>
		<picker @change="bindPickerChange2" :value="index2" :range="allDepartment" :range-key="'departmentName'">
			<view class="uni-input" :class="index2 == 0? 'uni-input':''">{{allDepartment[index2].departmentName}}</view>
		</picker>
		</view>
		
		<view class="divider"/>
		
		<!-- 工区 -->
		<view class="uni-list" v-if="IfWorkRegion">
			<view style="margin-top: 30px;">工区</view>
		<picker @change="bindPickerChange" :value="index" :range="allWorkRegion" :range-key="'regionName'">
			<view class="uni-input" :class="index == 0? 'uni-input':''">{{allWorkRegion[index].regionName}}</view>
		</picker>
		</view>
		<view class="divider"/>
		
<!-- 		<view class="form-item">
			<text>部门</text>
			<input v-model="department" placeholder="请输入部门" />
		</view>
		<view class="form-item">
			<text>工区</text>
			<input v-model="workArea" placeholder="请输入工区" />
		</view> -->
		<view class="form-item" style="margin-top: 30px;">
			<button @click="saveInfo">保存</button>
		</view>
	</view>
</template>




<script>
export default {
	data() {
		return {
			user:'',
			realName:"",
			department:"",
			workRegion:"",
			index:0,
			index2:0,
			allWorkRegion:[{regionName:"请选择工区",id:-1}],
			allDepartment:[
				{departmentName:"请选择部门", id:-1},
				],
			IfWorkRegion:false,
				
			companys:["广州市佑安土木工程有限公司", "汕尾市佑安沥青技术有限公司"],
			index3:0,
		};
	},
	methods: {
		saveInfo() {
			// 保存信息逻辑，比如调用API或者存储在本地
			// 然后返回上一页面
			let that = this;
			
			let data = {}
			if(that.realName.length == 0 || that.index2 <= 0 || (that.IfWorkRegion && that.index <= 0)){
				uni.showToast({
					icon: "error",
					title: "请输入完整信息"
				})
				return
			}
			data.realName = that.realName
			data.department = that.allDepartment[that.index2].id
			if(that.IfWorkRegion){
				data.workRegion = that.allWorkRegion[that.index].id
			}else{
				data.workRegion = 0
			}
			data.company = that.companys[that.index3]
			data.userId = that.user.userId
			
			that.ajax(that.url.updateUser,"PUT",data,function(resp){
				if(resp.data.code == 200){
					uni.showToast({
						title:"成功"
					})	
				}
				uni.navigateBack();
			})
			
		},
		
		async findAllRegioWork(j){
			let that=this
			that.allWorkRegion = [{regionName:"请选择工区",id:-1}]
			// console.log("department" + that.department)

			that.index == 0
			that.ajax(that.url.findAllRegioWork+"/"+ that.department,"GET",null,function(resp){
				// console.log(resp.data.data)
				// that.allWorkRegion.push(resp.data.data)
				let i = 0;
				if(resp.data.data.length == 0){
					that.IfWorkRegion = false
					return
				}else{
					for(i = 0; i < resp.data.data.length; i++){
						// console.log(resp.data.data[i])
						that.allWorkRegion.push(resp.data.data[i])
					}
					that.IfWorkRegion = true
				}
				if(j == 1){
					that.allWorkRegion.forEach((i,index)=>{
					console.log(that.allWorkRegion.length)
					if(i.id === that.workRegion){
						that.index = index
					}
				})
				}
				
			})
		},
		async findAllDepartment(){
			let that=this
			that.ajax(that.url.findAllDepartment,"GET",null,function(resp){
				// console.log(resp.data.data)
				let i = 0;
				for(i = 0; i < resp.data.data.length; i++){
					// console.log(resp.data.data[i])
					that.allDepartment.push(resp.data.data[i])
				}
				that.allDepartment.forEach((d,index)=>{
					console.log(that.allDepartment.length)
					if(d.id === that.department){
						that.index2 = index
					}
				})
			})
		},
		bindPickerChange: function(e){
			let that = this
			this.index = e.detail.value
			this.workRegion = that.allWorkRegion[e.detail.value].id
		},
		bindPickerChange2: function(e){
			let that = this
			this.index2 = e.detail.value;
			this.department = that.allDepartment[e.detail.value].id
			this.findAllRegioWork(0)
		},
		bindPickerChange3: function(e){
			this.index3 = e.detail.value;
		},
	},
	
	async onShow(){
		let that = this
		this.realName = this.user.realName
		if(this.user.company == "广州市佑安土木工程有限公司"){
			this.index3 = 0;
		}else{
			this.index3 = 1;
		}
		// this.index2 =  this.allDepartment.findIndex(w => w.id === that.department);
		await this.findAllDepartment();

		if(this.user.workRegion != null && this.user.workRegion ){
		 	await this.findAllRegioWork(1)
			// console.log(this.department)
			// this.index = this.allWorkRegion.findIndex(w => w.id === this.user.workRegion);
		}
	},
	
	async onLoad(options) {
		this.user = JSON.parse(decodeURIComponent(options.info));
		this.department = this.user.department
		if(this.user.workRegion != 0){
			this.workRegion = this.user.workRegion
		
		}
	}
};
</script>


<style lang="less">
@import url("updataMy.less");
</style>
